home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / processes / procdoggie / procdoggie.p < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.6 KB  |  317 lines

  1. {
  2.     File:        ProcDoggie.p
  3.  
  4.     Contains:    Main program file for the ProcDoggie application.
  5.  
  6.     Written by: Forrest Tanaka    
  7.  
  8.     Copyright:    Copyright © 1988-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. }
  23. PROGRAM ProcDoggie;
  24.  
  25. {[j=20/57/1$] Pasmat Options}
  26.     
  27.  
  28. (*******************************************************************************
  29. * Used Units
  30. *******************************************************************************)
  31.  
  32.     USES
  33.         AppleEvents
  34.         ,Fonts
  35.         ,ToolUtils
  36.         ,DiskInit
  37.         ,SegLoad
  38.         ,GestaltEqu
  39.         
  40.         (* Application *)
  41.         ,UGlobals
  42.         ,UEmergMem
  43.         ,UProcessUtils
  44.         ,UMenuHandler
  45.         ,UProcessGuts
  46.         ,UEvents
  47.         ;
  48.  
  49.  
  50. (*******************************************************************************
  51. * Constants
  52. *******************************************************************************)
  53.  
  54.     CONST
  55.         kBecomingActive = TRUE; {Pass to DoActivateEvt; indicates becoming active}
  56.  
  57.  
  58. (*******************************************************************************
  59. * Global Variables
  60. *******************************************************************************)
  61.  
  62.     VAR
  63.         gProcessListWind: WindowPtr; {Pointer to the process list window}
  64.  
  65.  
  66. {$S Main}
  67. (*******************************************************************************
  68. * DoneRequiredParams - Done processing required params; OK?
  69. *
  70. * DoneRequiredParams checks to see if the AppleEvent specified by the
  71. * anAppleEvent parameter has any required parameters that we haven’t yet
  72. * processed.  If there aren’t any left, then noErr is returned.  If there are
  73. * required parameters that haven’t been processed yet, then errAEEventNotHandled
  74. * is returned.  If any other errors occur, then that error code is returned.
  75. *******************************************************************************)
  76.  
  77.     FUNCTION DoneRequiredParams (anAppleEvent: AppleEvent): OSErr;
  78.  
  79.         VAR
  80.             typeCode:   DescType; {Type of AppleEvent attribute found; ignored}
  81.             actualSize: Size;     {Actual size of parameters; ignored}
  82.             error:      OSErr;
  83.  
  84.     BEGIN
  85.         (* Are there any required parameters in AppleEvent we didn’t process? *)
  86.            error := AEGetAttributePtr (anAppleEvent, keyMissedKeywordAttr,
  87.                     typeWildCard, (*<*)typeCode, NIL, 0, (*<*)actualSize);
  88.             IF error = errAEDescNotFound THEN
  89.                 (* No required parameters left, so no error *)
  90.                 DoneRequiredParams := noErr
  91.             ELSE IF error = noErr THEN
  92.                 (* There was at least one required parameter we didn’t process *)
  93.                 DoneRequiredParams := errAEEventNotHandled
  94.             ELSE
  95.                 (* Some other error happened *)
  96.                 DoneRequiredParams := error
  97.     END;
  98.  
  99.  
  100. {$S Main}
  101. (*******************************************************************************
  102. * HandleAEquit - Handler for 'quit' AppleEvent
  103. *
  104. * This is the AppleEvent handler for the 'quit' AppleEvent as passed in the
  105. * quitAppleEvent parameter by the AppleEvent Manager.  The DoQuit routine is
  106. * called which causes this application to quit at the start of the next
  107. * iteration of the main event loop.
  108. *
  109. * Though the quit AppleEvent doesn’t contain any parameters, the standard thing
  110. * to do in reaction to any AppleEvent is to check to see if there are any
  111. * required parameters in the AppleEvent that this routine doesn’t recognise.
  112. * DoneRequiredParms checks for this condition and returns an error if there are
  113. * in fact required parameters in the AppleEvent or if some other error occurs
  114. * during the check.
  115. *******************************************************************************)
  116.  
  117.     FUNCTION HandleAEquit (VAR quitAppleEvent: AppleEvent;
  118.                            VAR reply:          AppleEvent;
  119.                            handlerRefCon:  LongInt): OSErr;
  120.  
  121.         VAR
  122.             error: OSErr;
  123.  
  124.         PROCEDURE RecoverError (errorCode: OSErr);
  125.  
  126.         BEGIN
  127.             HandleAEquit := errorCode;
  128.             EXIT (HandleAEquit)
  129.         END;
  130.  
  131.     BEGIN
  132.         {$unused reply}
  133.         {$unused handlerRefCon}
  134.         (* quit AE has no parms, but check in case the client requires any *)
  135.         error := DoneRequiredParams (quitAppleEvent);
  136.         IF error <> noErr THEN
  137.             RecoverError (error);
  138.  
  139.         (* Handle the Quit command *)
  140.         error := DoQuit;
  141.         IF error <> noErr THEN
  142.             RecoverError(error);
  143.  
  144.         HandleAEquit := noErr
  145.     END;
  146.  
  147.  
  148. {$S Startup}
  149. (*******************************************************************************
  150. * StartUp - Do whatever has to be done to initialize the application
  151. *
  152. * This routine is called after the heap is initialized to initialize the
  153. * application.  This involves initializing the toolbox, emergency memory, and
  154. * loading up the menus.  If any errors occur while doing this, StartUp displays
  155. * an alert telling the user what the error was and then ExitToShell is called.
  156. * This is an unusual way to react to errors, and I only do it here because it’s
  157. * so early in execution that there really isn’t much else that can be done.
  158. *
  159. * See this UEmergMem unit in this application for details about emergency
  160. * memory.
  161. *******************************************************************************)
  162.  
  163.     PROCEDURE StartUp;
  164.  
  165.         CONST
  166.             kSysHandler = TRUE; {Specifies that AE handler is in system heap}
  167.  
  168.         VAR
  169.             error: OSErr;
  170.  
  171.         PROCEDURE HandleError (messageClass: Integer;
  172.                                messageIndex: Integer);
  173.  
  174.             VAR
  175.                 junkError : OSErr;
  176.                 junkItemHit: Integer; {Result of alert; ignored}
  177.  
  178.         BEGIN
  179.             junkError := ShowStopAlert (messageClass, messageIndex, junkItemHit);
  180.             ExitToShell
  181.         END;
  182.         
  183.         VAR
  184.             gestaltResponse: LongInt;
  185.  
  186.     BEGIN
  187.         (* Initialize the toolbox *)
  188.         InitGraf (@qd.thePort);
  189.         InitFonts;
  190.         InitWindows;
  191.         InitMenus;
  192.         TEInit;
  193.         InitDialogs (NIL);
  194.         
  195.         (* Check environment. *)
  196.         IF (Gestalt(gestaltSystemVersion, gestaltResponse) <> noErr) | ( gestaltResponse < $0700) THEN
  197.             HandleError(rMiscErrMessages, kMiscSystemTooSmall);
  198.  
  199.         (* We make the assumption that if we have System 7 then we have a bunch of other
  200.             System 7 features that we rely on, namely:
  201.             
  202.             o new Standard File calls
  203.             o AppleEvents
  204.             o Process Manager
  205.         
  206.           While this is counter to the philosophy of Gestalt, it sure makes the code smaller.
  207.             It's also a sensible approach for "big bang" system releases like System 7.
  208.             Besides, ProcDoggie can do nothing useful without the Process Manager (which
  209.             was introduced with System 7), so there's no point in us 'limping along',
  210.             using old style Standard File calls and so on, when we can't display a
  211.             process list.
  212.         *)
  213.         
  214.         (* Initialise some boring parts of our application.  Basically this involves
  215.             making some UPPs.
  216.         *)
  217.         InitGlobals;
  218.         InitProcessGuts;
  219.  
  220.         (* Initialize emergency memory *)
  221.         InitEmergMem;
  222.         IF FailLowMemory (0) THEN
  223.             HandleError (rMemErrMessages, kMemErrAppOpenMsg);
  224.  
  225.         (* Load the menus and draw the menu bar *)
  226.         StartMenus;
  227.         IF FailLowMemory (0) THEN
  228.             HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  229.         ELSE IF gError <> noErr THEN
  230.             IF gError = memFullErr THEN
  231.                 HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  232.             ELSE IF gError = resNotFound THEN
  233.                 HandleError (rResErrMessages, kResErrAppDamageMsg)
  234.             ELSE
  235.                 HandleError (rMiscErrMessages, kMiscErrUnknownMsg);
  236.  
  237.         (* Install the AppleEvent handler *)
  238.         error := AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
  239.                 NewAEEventHandlerProc(@HandleAEquit), 0, NOT kSysHandler);
  240.         IF (error = memFullErr) | FailLowMemory (0) THEN
  241.             HandleError (rMemErrMessages, kMemErrAppOpenMsg)
  242.         ELSE IF error <> noErr THEN
  243.             HandleError (rMiscErrMessages, kMiscErrUnknownMsg)
  244.     END;
  245.  
  246.  
  247. {$S Main}
  248. (*******************************************************************************
  249. * EventLoop - Main event loop for this application
  250. *
  251. * This is the main event loop of this application.  During every iteration of
  252. * the event loop, the menus are kept up-to-date, and the Process List window and
  253. * all of the open Process Information windows are given time to update
  254. * themselves to current conditions.  Also, NoEmergMem is called to detect
  255. * whether the emergency memory was used.  If it was, then RecoverEmergMem is
  256. * called in an attept to get it back.  If it can’t, then some commands could be
  257. * disabled until the memory can be recovered.
  258. *******************************************************************************)
  259.  
  260.     PROCEDURE EventLoop;
  261.  
  262.         VAR
  263.             anEvent: EventRecord; {An incoming event}
  264.  
  265.     BEGIN
  266.         FixMenus;
  267.         InitCursor;
  268.         gQuitting := FALSE;
  269.  
  270.         (* We loop “forever,” or until the Quit handler calls ExitToShell *)
  271.         WHILE NOT gQuitting DO
  272.             BEGIN
  273.                 (* Give all open windows some time *)
  274.                 IdleAllProcessWindows;
  275.  
  276.                 (* Try to reallocate emergency memory if it’s been used *)
  277.                 IF NoEmergMem THEN
  278.                     RecoverEmergMem;
  279.  
  280.                 (* Fix the menus to reflect current conditions *)
  281.                 FixMenus;
  282.  
  283.                 (* It’s time to get and examine an event *)
  284.                 IF WaitNextEvent (everyEvent, (*<*)anEvent, kMaxSleepTime, NIL) THEN
  285.                     BEGIN
  286.                         DoEvent(anEvent);
  287.                     END
  288.             END
  289.     END;
  290.  
  291.  
  292. BEGIN
  293.     (* Set up the heap *)
  294.     MaxApplZone;
  295.     MoreMasters;
  296.     MoreMasters;
  297.     MoreMasters;
  298.     MoreMasters;
  299.     MoreMasters;
  300.     MoreMasters;
  301.  
  302.     (* Do anything that must be done at program start-up *)
  303.     StartUp;
  304.     {$ifc not GENERATINGCFM}
  305.         UnloadSeg (@StartUp);
  306.     {$endc}
  307.  
  308.     (* Set the default launch mode *)
  309.     SetLaunchMode (kJustLaunch);
  310.  
  311.     (* Open the process list window *)
  312.     gProcessListWind := CreateProcessListWindow;
  313.  
  314.     (* Enter the main event loop *)
  315.     EventLoop
  316. END.
  317.